#include <avr/io.h>

#include "spi.h"

bool
SPI::Connect()
{
	if (mode == MASTER)
	{
		BCLR(SPCR,SPE);		//
		BSET(DDRB,MCU_CS);	//
		ReleaseSlave();
		BSET(SPCR,MSTR);	// set as SPI master
		BSET(SPCR,SPE);		// enable SPI
	} else {
		
		BCLR(DDRB,MCU_CS);
		BSET(SPCR,SPE);
	}
	return true;
}

char
SPI::TxRx(volatile char c)
{
	SPDR = c;
	while( !(SPSR & (1<<SPIF)) );
	return SPDR;
}

